<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>商品分类管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="/css/style.css">
	<script src="/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="/plugins/bootstrap/js/bootstrap.min.js"></script>
	<link rel="stylesheet" href="/plugins/select2/select2.css" />
	<link rel="stylesheet" href="/plugins/select2/select2-bootstrap.css" />
	<script src="/plugins/select2/select2.min.js" type="text/javascript"></script>
    
</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品分类管理     
                       	</h3>
                    </div>

                    <div class="box-body">
                  			 <ol class="breadcrumb" id="catOl">
                        		<li>
		                        	<a href="javascript:listCat(0)" >顶级分类列表</a>
		                        </li>
	                        </ol>

                        <!-- 数据表格 -->
                        <div class="table-box">
							
                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" onclick="toTop()" data-target="#addModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="delCats()" ><i class="fa fa-trash-o"></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="刷新" ><i class="fa fa-check"></i> 刷新</button>
                                       
                                    </div>
                                </div>
                            </div>                          

			                <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">分类ID</th>
									      <th class="sorting">分类名称</th>									   
									      <th class="sorting">类型模板ID</th>
									     						
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="cats">
			                          <tr >
			                              <td><input  type="checkbox" ></td>			                              
				                          <td>982</td>
									      <td>吊坠/项链</td>									    
									      <td>
									      	11    
									      </td>									      
		                                  <td class="text-center">		                                     
		                                      <button type="button" class="btn bg-olive btn-xs" >查询下级</button> 		                                     
		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal" >修改</button>                                           
		                                  </td>
			                          </tr>
									  <tr >
			                              <td><input  type="checkbox"> </td>			                              
				                          <td>983</td>
									      <td>手镯/手链/脚链</td>									    
									      <td>
									      	11    
									      </td>									      
		                                  <td class="text-center">		                                     
		                                      <button type="button" class="btn bg-olive btn-xs" >查询下级</button> 		                                     
		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal" >修改</button>                                           
		                                  </td>
			                          </tr>

			                      </tbody>
			                  </table>
			                  <!--数据列表/-->                      
						
                        </div>
                        <!-- 数据表格 /-->
                        
                        
                        
                        
                     </div>
                    <!-- /.box-body -->
              
                                
<!-- 新增窗口 -->
<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">商品分类新增</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
				<tr>
		      		<td>上级商品分类</td>
		      		<td id="topName">
		      		   珠宝 >>  银饰
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>商品分类名称</td>
		      		<td><input id="catName" class="form-control" placeholder="商品分类名称">  </td>
		      	</tr>			  
		      	<tr>
		      		<td>类型模板</td>
		      		<td id="typeBody">
<!--		      			<input select2 config="options['type_template']" placeholder="商品类型模板" class="form-control" type="text"/>-->
		      		</td>		      		      		
		      	</tr>		      	
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" onclick="addCat()" aria-hidden="true">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>
  <div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  </div>

<script>
	$(function () {
		listCat()
		listTemplate()
		listTemplate1()
	})

	function addCat() {
		$.ajax({
			url:"/cat/addCat",
			data:{typeId:$("#typeSelect").val(),name:$("#catName").val(),parentId:$("#parentId").val()},
			dataType:"json",
			type:"post",
			async:false,
			success:function (result) {
				if (result.code == 10000){
					alert(result.message)
					window.location.reload();
				}else {
					alert(result.message)
				}
			},
			error:function (result) {
				alert("系统错误")
			}
		})
	}

	function listCat(parentId) {
		if (parentId==null){
			parentId = 0
		}
		$.ajax({
			url:"/cat/listCat",
			data:{parentId:parentId},
			type:"post",
			dataType:"json",
			async:false,
			success:function (result) {
				var cats = result.data;
				var html = '<input type="hidden" id="parentId" value="'+parentId+'">';
				if (result.code == 10000){
					$(cats).each(function (i,e) {
						html+='<tr>'
						html+='<td><input name="box" value="'+e.id+'" type="checkbox" ></td>'
						html+='<td>'+e.id+'</td>'
						html+='<td>'+e.name+'</td>'
						html+='<td>'+e.typeId+'</td>'
						html+='<td class="text-center">'
						html+='<button type="button" onclick="listCat('+e.id+')" class="btn bg-olive btn-xs" >查询下级</button>'
						html+='<button type="button" class="btn bg-olive btn-xs" data-toggle="modal" onclick="toUpdate('+e.id+')" data-target="#editModal" >修改</button>'
						html+='</td>'
						html+='</tr>'
					})
					$("#cats").html(html);

					topType(parentId)
				}else {
					html+='<h1>'+result.message+'</h1>'
					$("#cats").html(html)
				}

			},
			error:function (result) {
				alert("系统错误")
			}
		})
	}
	function toUpdate(id) {
		$.ajax({
			url:"/cat/toUpdate",
			data:{id:id},
			dataType:"json",
			type:"post",
			async:false,
			success:function (result) {
				var html =''
				var catList = result.data
				html+='<div class="modal-dialog" >'
				html+='<div class="modal-content">'
				html+='<div class="modal-header">'
				html+='<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>'
				html+='<h3 id="myModalLabel">商品分类编辑</h3>'
				html+='</div>'
				html+='<div class="modal-body">'
				html+='<table class="table table-bordered table-striped"  width="800px">'
				html+='<tr>'
				html+='<td>上级商品分类</td>'
				html+='<td id="topName1">珠宝 >>  银饰</td>'
				html+='</tr>'
				html+='<tr>'
				html+='<td>商品分类名称</td>'
				html+='<td><input id="catName1" value="'+catList.name+'" class="form-control" placeholder="商品分类名称">  </td>'
				html+='</tr>'
				html+='<tr>'
				html+='<td>类型模板</td>'
				html+='<td id="typeBody1">'
				html+='</td>'
				html+='</tr>'
				html+='</table>'
				html+='</div>'
				html+='<div class="modal-footer">'
				html+='<button class="btn btn-success" onclick="update('+catList.id+')" data-dismiss="modal" aria-hidden="true">保存</button>'
				html+='<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>'
				html+='</div>'
				html+='</div>'
				html+='</div>'

				$("#editModal").html(html)

				listTemplate1()
				toTop1()

				// var arr = []
				// arr.push(catList.typeId)
				$("#typeSelect1").val(catList.typeId).trigger("change")

			},
			error:function (result) {
				alert("系统错误")
			}
		})
	}

	function update(id) {
		$.ajax({
			url:"/cat/update",
			data:{id:id,typeId:$("#typeSelect1").val(),name:$("#catName1").val()},
			dataType:"json",
			type:"post",
			async:false,
			success:function (result) {
				if (result.code == 10000){
					alert(result.message)
					window.location.reload();
				}else {
					alert(result.message)
				}
			},
			error:function (result) {
				alert("系统错误")
			}
		})
	}
	function toTop1() {
		$.ajax({
			url:"/cat/topType",
			data:{id:$("#parentId").val()},
			type:"post",
			dataType:"json",
			async:false,
			success:function (result) {
				var cats = result.data;
				if (result.code == 10000){
					var html = ''
					$(cats).each(function (i,e) {
						html+= e.name + "->"
					})
					$("#topName1").html(html)
				}else {
					alert(result.message)
				}
			},
			error:function (result) {
				alert("系统错误")
			}
		})
	}

	function toTop() {
		$.ajax({
			url:"/cat/topType",
			data:{id:$("#parentId").val()},
			type:"post",
			dataType:"json",
			async:false,
			success:function (result) {
				var cats = result.data;
				if (result.code == 10000){
					var html = ''
					$(cats).each(function (i,e) {
						html+= e.name + "->"
					})
					$("#topName").html(html)
				}else {
					alert(result.message)
				}
			},
			error:function (result) {
				alert("系统错误")
			}
		})
	}
	function topType(id) {
		if (id == 0){
			html = ''
			html+='<li>'
			html+='<a href="javascript:listCat(0)" >顶级分类列表</a>'
			html+='</li>'
			$("#catOl").html(html);
		}

		$.ajax({
			url:"/cat/topType",
			data:{id:id},
			type:"post",
			dataType:"json",
			async:false,
			success:function (result) {
				var cats = result.data;
				if (result.code == 10000){
						var html = '';
						html+='<li>'
						html+='<a href="javascript:listCat(0)" >顶级分类列表</a>'
						html+='</li>'
						$(cats).each(function (i,e) {
							html+='<li>'
							html+='<a href="javascript:listCat('+e.id+')" >'+e.name+'</a>'
							html+='</li>'
						})
						$("#catOl").html(html)
				}else {
					alert(result.message)
				}
			},
			error:function (result) {
				alert("系统错误")
			}
		})
	}
	function listTemplate() {
		$.ajax({
			url:"/typeTemplate/listTemplate",
			data:{},
			dataType:"json",
			type:"post",
			async:false,
			success:function (result) {
				var templates = result.data
				if (result.code == 10000){
					var html = '<select id="typeSelect" style="width:100%">'
					$(templates).each(function (i,e) {
						html+='<option value="'+e.id+'">'+e.name+'</option>'
					})
					html+='</select>'
					$("#typeBody").html(html)
					$("#typeSelect").select2()
				}else {
					alert(result.message)
				}
			},
			error:function (result) {
				alert("系统错误")
			}
		})
	}
	function listTemplate1() {
		$.ajax({
			url:"/typeTemplate/listTemplate",
			data:{},
			dataType:"json",
			type:"post",
			async:false,
			success:function (result) {
				var templates = result.data
				if (result.code == 10000){
					var html = '<select id="typeSelect1" style="width:100%">'
					$(templates).each(function (i,e) {
						html+='<option value="'+e.id+'">'+e.name+'</option>'
					})
					html+='</select>'
					$("#typeBody1").html(html)
					$("#typeSelect1").select2()
				}else {
					alert(result.message)
				}
			},
			error:function (result) {
				alert("系统错误")
			}
		})
	}
	function delCats() {
		if(confirm("确定要删除本节点及以下的所有节点吗")){
			var boxs = document.getElementsByName("box")
			var ids = ''
			$(boxs).each(function (i,e) {
				if (e.checked){
					ids+= $(e).val()+","
				}
			})
			ids = ids.substring(0,ids.length-1)
			$.ajax({
				url:"/cat/delCats",
				data:{ids:ids},
				type:"post",
				dataType:"json",
				async:false,
				success:function (result) {
					alert(result.message)
					window.location.reload();
				},
				error:function (result) {
					alert("系统错误")
				}
			})
		}
	}
</script>
</body>
</html>